<?php

declare(strict_types=1);

namespace WanZ\App\User\Action;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use WanZ\App\User\Logic\SmsLogic;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;

/**
 * 发送短信信息
 * @Controller
 */
class SendCode extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var SmsLogic
     */
    private $smsLogic;
        
    /**
     * @RequestMapping(path="/auth/sendChangeInfo", methods="GET")
     * @return array 返回数组
     */
    public function handle(): array
    {       
        ## 参数验证
        $this->validated($this->request->all());

        ## 请求参数
        $phone = (string) $this->request->input('phone');
        $companyId = (int) $this->request->input('company_id');
		$type = (string) $this->request->input('type', 'reset_wx_account');

		$this->smsLogic->sendCode($phone, $type, $companyId);

		return [];
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
            'phone' => 'required|size:11|phone|bail',
            'company_id' => 'required|integer|min:1|bail',
            'type' => 'string|in:reset_ali_account,reset_wx_account|bail',
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
            'phone.required' => '手机号码 必填',
            'phone.size' => '手机号码 字符串长度为固定值：11',
            'phone.phone' => '手机号码 格式不正确',
            'company_id.required' => '公司ID 必填',
            'company_id.integer' => '公司ID 必需为整数',
            'company_id.min' => '公司ID 值必须大于1',
            'type.string' => '短信类型 必需为字符串',
            'type.in' => '短信类型 非法 值必须在列表内：[reset_ali_account,reset_wx_account]',
        ];
    }
}